describe('SMT_SIDE验证规则测试', () => {
  let testData

  before(() => {
    cy.fixture('test-data').then((data) => {
      testData = data
    })
  })

  describe('🔧 smtSide字段验证测试', () => {
    const smtSideVariations = [
      { value: '1', description: '数字1' },
      { value: '2', description: '数字2' },
      { value: 'A', description: '字母A' },
      { value: 'B', description: '字母B' },
      { value: 'TOP', description: 'TOP字符串' },
      { value: 'BOTTOM', description: 'BOTTOM字符串' },
      { value: 'T', description: '字母T' },
      { value: 'L', description: '字母L（下层）' }
    ]

    smtSideVariations.forEach((testCase) => {
      it(`应该接受smtSide值: ${testCase.value} (${testCase.description})`, () => {
        const timestamp = Date.now()
        const uniqueOrderNo = `SMTTEST${timestamp}`
        
        const operatorConfirmRequest = {
          orderNo: uniqueOrderNo,
          orderBranchNo: "01",
          mounterId: "M05",
          smtSide: testCase.value, // 测试不同的smtSide值
          operatorId: testData.operators.valid.operatorId,
          lineId: "LINE01"
        }

        cy.log(`🧪 测试smtSide = "${testCase.value}" (${testCase.description})`)

        cy.apiRequest('POST', '/api/smt/saveOperatorConfirm', operatorConfirmRequest)
          .then((response) => {
            expect(response.status).to.equal(200)
            
            if (response.body.code === testData.responseCodes.success) {
              cy.log(`✅ 接受smtSide = "${testCase.value}" - 数据库INSERT成功`)
            } else if (response.body.code === testData.responseCodes.orderNotFound) {
              cy.log(`📋 订单不存在但smtSide验证通过 - "${testCase.value}"被接受`)
            } else if (response.body.code === testData.responseCodes.badRequest) {
              cy.log(`❌ 意外：smtSide = "${testCase.value}" 被拒绝: ${response.body.message}`)
              // 如果仍然被拒绝，说明可能还有其他验证规则
            } else {
              cy.log(`📝 smtSide = "${testCase.value}" 验证结果: ${response.body.code} - ${response.body.message}`)
            }
            
            // 主要验证点：不应该返回smtSide相关的验证错误
            expect(response.body.message).to.not.include('实装面只能是1或2')
            expect(response.body.message).to.not.include('实装面参数无效')
          })
      })
    })

    it('应该拒绝空的smtSide值', () => {
      const invalidRequest = {
        orderNo: "TEST123",
        orderBranchNo: "01",
        mounterId: "M05",
        smtSide: "", // 空值应该被拒绝
        operatorId: testData.operators.valid.operatorId,
        lineId: "LINE01"
      }

      cy.apiRequest('POST', '/api/smt/saveOperatorConfirm', invalidRequest)
        .then((response) => {
          expect(response.status).to.equal(200)
          expect(response.body.code).to.equal(testData.responseCodes.badRequest)
          expect(response.body.message).to.include('实装面不能为空')
          cy.log('✅ 正确拒绝空的smtSide值')
        })
    })

    it('应该拒绝null的smtSide值', () => {
      const invalidRequest = {
        orderNo: "TEST123",
        orderBranchNo: "01", 
        mounterId: "M05",
        smtSide: null, // null值应该被拒绝
        operatorId: testData.operators.valid.operatorId,
        lineId: "LINE01"
      }

      cy.apiRequest('POST', '/api/smt/saveOperatorConfirm', invalidRequest)
        .then((response) => {
          expect(response.status).to.equal(200)
          expect(response.body.code).to.equal(testData.responseCodes.badRequest)
          expect(response.body.message).to.include('实装面不能为空')
          cy.log('✅ 正确拒绝null的smtSide值')
        })
    })
  })
})